#include "precompile.h"

#include "document/IPage.h"
#include "LayoutService.h"
#include "Layout.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

LayoutService::LayoutService()
{
}

LayoutService::~LayoutService()
{
}

Points LayoutService::LayoutPages(IObject* root,Graphics* graphics,const Size& clientSize)
{
	PageLayoutInfo layoutinfo;

	sys::ServiceInterfacePtr<IHierarchyService> pIFHierarchyService(HIERARCHY_SERVICE);
	objectarray pages;
	pIFHierarchyService->GetPages(root,pages);
	sys::InterfacePtr<IPage> pIFPage(pages[0]);
	Size pagesize(graphics->UnitMMtoLine(pIFPage->getPageWidth()),
							 graphics->UnitMMtoLine(pIFPage->getPageHeight()));
	PageLayout layout(clientSize,pagesize,pages.size(),Point(0,0));
	layout.Layout();
	layoutinfo = layout.GetResult();

	Points pts;
	const PageLayoutInfo& layoutInfo = layout.GetResult();
	for (int i  =0; i < layoutInfo.pages.size() && i < pages.size(); ++i)
	{
		const PageLayoutEntry& entry = layoutInfo.pages[i];
		const Point& viewpoint = entry.viewpoint;
		pts.push_back(viewpoint);
	}

	return pts;
}